#include <Windows.h>
#include "application.h"
#include <d3dx9.h>

TApplication       app;
LPDIRECT3D9        g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9  g_pd3dDevice = NULL; // Our rendering device


void TApplication::loadConfig( ) {
	xres = 800;
	yres = 600;
}

bool isPressed( int key ) {
	return ::GetAsyncKeyState( key ) & 0x8000;
}


//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT TApplication::initD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	 //d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
	 //d3dpp.BackBufferWidth = app.xres;    // set the width of the buffer
    //d3dpp.BackBufferHeight = app.yres;    // set the height of the buffer

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

	onBoot();

    return S_OK;
}

// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID TApplication::cleanup()
{
    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
}


//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
	case WM_DESTROY:
		app.cleanup();
		PostQuitMessage( 0 );
		return 0;
	}

	return DefWindowProc( hWnd, msg, wParam, lParam );
}

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
	UNREFERENCED_PARAMETER( hInst );

	// Register the window class
	WNDCLASSEX wc =
	{
		sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
		GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
		"D3D Tutorial", NULL
	};
	RegisterClassEx( &wc );

	app.loadConfig();

	// Create the application's window
	HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 03: Matrices",
		WS_OVERLAPPEDWINDOW, 100, 100, app.xres, app.yres,
		NULL, NULL, wc.hInstance, NULL );

	/*HWND hWnd = CreateWindowEx(NULL,
                          "D3D Tutorial",
                          "D3D Tutorial 03: Matrices",
                           WS_EX_TOPMOST | WS_POPUP,
                          0, 0,
                          app.xres, app.yres,    // set window to new resolution
                          NULL,
                          NULL,
                          wc.hInstance,
                          NULL);*/

	// Initialize Direct3D
	if( SUCCEEDED( app.initD3D( hWnd ) ) )
	{
		// Show the window
		ShowWindow( hWnd, SW_SHOWDEFAULT );
		UpdateWindow( hWnd );

		// Enter the message loop
		MSG msg;
		ZeroMemory( &msg, sizeof( msg ) );
		while( msg.message != WM_QUIT )
		{
			if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
			{
				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
			else {
				float delta_time = 1.0f / 60.0f;
				app.update( delta_time );
				app.render();
			}
		}
	}

	UnregisterClass( "D3D Tutorial", wc.hInstance );
	return 0;


}
